package com.ruoyi.project.paea.paeaDsTrade.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.CommonParaTool;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.er.domain.PaeaErError;
import com.ruoyi.project.paea.paeaDsTrade.domain.PaeaDsTrade;
import com.ruoyi.project.paea.paeaDsTrade.model.TradeTellerImagesVo;
import com.ruoyi.project.paea.paeaDsTrade.service.IPaeaDsTradeService;
import com.ruoyi.project.sdi.sdiImage.domain.SdiImage;
import com.ruoyi.project.sdi.sdiImage.service.ISdiImageService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 交易流水Controller
 *
 * @author 关晓强
 * @date 2020-03-16
 */
@Controller
@RequestMapping("/paea/paeaDsTrade")
public class PaeaDsTradeController extends BaseController
{
    private String prefix = "paea/paeaDsTrade";

    @Autowired
    private IPaeaDsTradeService paeaDsTradeService;

    @Autowired
    private ISdiImageService sdiImageService;

    @RequiresPermissions("paea:paeaDsTrade:view")
    @GetMapping()
    public String paeaDsTrade()
    {
        return prefix + "/paeaDsTrade";
    }

    /**
     * 查询交易流水列表
     */
    @RequiresPermissions("paea:paeaDsTrade:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PaeaDsTrade paeaDsTrade)
    {
        startPage();
        List<PaeaDsTrade> list = paeaDsTradeService.list(new QueryWrapper<>(paeaDsTrade));
        return getDataTable(list);
    }

    /**
     * 导出交易流水列表
     */
    @RequiresPermissions("paea:paeaDsTrade:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PaeaDsTrade paeaDsTrade)
    {
        List<PaeaDsTrade> list = paeaDsTradeService.list(new QueryWrapper<>(paeaDsTrade));
        ExcelUtil<PaeaDsTrade> util = new ExcelUtil<PaeaDsTrade>(PaeaDsTrade.class);
        return util.exportExcel(list, "paeaDsTrade");
    }

    /**
     * 新增交易流水
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存交易流水
     */
    @RequiresPermissions("paea:paeaDsTrade:add")
    @Log(title = "交易流水", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PaeaDsTrade paeaDsTrade)
    {
        return toAjax(paeaDsTradeService.saveOrUpdate(paeaDsTrade));
    }

    /**
     * 修改交易流水
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        PaeaDsTrade paeaDsTrade = paeaDsTradeService.getById(id);
        mmap.put("paeaDsTrade", paeaDsTrade);
        return prefix + "/edit";
    }

    /**
     * 修改保存交易流水
     */
    @RequiresPermissions("paea:paeaDsTrade:edit")
    @Log(title = "交易流水", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PaeaDsTrade paeaDsTrade)
    {
        return toAjax(paeaDsTradeService.saveOrUpdate(paeaDsTrade));
    }

    /**
     * 删除交易流水
     */
    @RequiresPermissions("paea:paeaDsTrade:remove")
    @Log(title = "交易流水", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(paeaDsTradeService.removeByIds(Arrays.asList(ids.split(","))));
    }

    /**
     * 交易流水详情
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap)
    {
        PaeaDsTrade paeaDsTrade = paeaDsTradeService.getById(id);
        List<SdiImage> sdiImageList = sdiImageService.getSdiImagesByPaeaDsTrade(paeaDsTrade);
        String imgPathPrefix = "";
        if (StringUtils.isNotEmpty(sdiImageList)) {
            imgPathPrefix = CommonParaTool.getImgPathPrefix(sdiImageList.get(0).getAuditOrgCode());
        }
        mmap.put("paeaDsTrade", paeaDsTrade);
        mmap.put("sdiImageList", sdiImageList);
        mmap.put("imgPathPrefix", imgPathPrefix);
        return prefix + "/detail";
    }

    /**
     * 当日柜员流水影像
     */
    @GetMapping("/tellerImages/{id}")
    public String tellerImages(@PathVariable("id") String id,ModelMap mmap)
    {
        TradeTellerImagesVo tradeTellerImagesVo = this.paeaDsTradeService.getTradeTellerImagesVo(id);
        mmap.put("tradeTellerImagesVo", tradeTellerImagesVo);
        return prefix + "/tellerImages";

    }

}
